package cn.taoticket.service.impl;

import cn.taoticket.client.RestTaoticketImageClient;
import cn.taoticket.client.RestTaoticketItemClient;
import cn.taoticket.common.Constants;
import cn.taoticket.common.EmptyUtils;
import cn.taoticket.pojo.TaoticketImage;
import cn.taoticket.pojo.TaoticketItem;
import cn.taoticket.service.HomeService;
import cn.taoticket.vo.HotItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class HomeServiceImpl implements HomeService {
    @Resource
    private RestTaoticketItemClient restTaoticketItemClient;
    @Resource
    private RestTaoticketImageClient restTaoticketImageClient;

    @Override
    public List<HotItemVo> queryBanner() throws Exception {
        //查询轮播图前五个
        Map<String, Object> param = new HashMap<>();
        param.put("isBanner", 1);
        param.put("beginPos", 0);
        param.put("pegeSize", 5);
        List<TaoticketItem> taoticketItemListByMap = restTaoticketItemClient.getTaoticketItemListByMap(param);
        //组装接口返回数据
        if (EmptyUtils.isEmpty(taoticketItemListByMap)) {
            return null;
        }
        List<HotItemVo> hotItemVoList = new ArrayList<>();
        for (TaoticketItem taoticketItem : taoticketItemListByMap) {
            HotItemVo hotItemVo = new HotItemVo();
            BeanUtils.copyProperties(taoticketItem, hotItemVo);//该方法的作用是如果两个对象有一样的字段则封装起来
            //查询图片信息
            List<TaoticketImage> taoticketImages = restTaoticketImageClient.queryImageList(taoticketItem.getId(),
                    Constants.Image.ImageType.carousel,
                    Constants.Image.ImageCategory.item);
            //组装图片信息
            hotItemVo.setImgUrl(EmptyUtils.isEmpty(taoticketImages) ? null : taoticketImages.get(0).getImgUrl());
            hotItemVoList.add(hotItemVo);
        }
        return hotItemVoList;
    }
}
